require('./index.scss');
var Tool = require('common/tool');
var subMenuList = require('../subMenuList');
require('comp/tab');
require('comp/dialog');
require('../../../components/enableAccount');

var vm = avalon.define({
    $id: 'myAccount',
    navShowArr:"",
    accountInfo:'',
    weixinMap:"",
    isBinding:true,
    tabList:{
        active:0,
        list:subMenuList
    },
    accountTab:{
        active:0,
        list:[],
        switchFn:function(_index){
            vm.accountTab.active = _index;
        }
    },
    emailDialog:{
        display:"none",
        title: '修改邮箱',
        step:1,
        email:'',
        emailError:'',
        agree:false,
        loading:false,
        /* 点击下一步 */
        nextStep(){
            var emailDialog = vm.emailDialog;
            var email_reg = /\w+((-w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+/;
            if(!emailDialog.email || emailDialog.email.length===0 || !email_reg.test(emailDialog.email)){
                emailDialog.emailError = '请输入邮箱地址';
                return false;
            }
            emailDialog.emailError = '';
            checkEmail(emailDialog);
            return false;
        }
    },
    unEnable:0,                                // 未激活激活的账号在accountList数组里面的索引
    hasEnable:0,                               // 已经激活的账号在accountList数组里面的索引， 通过hasEnable来查找已激活的账号
    /* 激活账号的弹窗数据 */
    enableDialog:{
        display:"none",
        title: '',
        close(){}
    },
    /* 激活账号 */
    enableAccount:function(data,index){
        vm.hasEnable = index===0 ? 1:0;
        vm.enableDialog.display = 'block';
        vm.enableDialog.title = '激活'+data.platform+'账号';
    }
});

vm.$watch('onReady',function(){
    var loop = setInterval(function(){
        if(rootVm.navShowArr.length>0){
            vm.navShowArr = rootVm.navShowArr;
            clearInterval(loop);
        }
    },200);
    new MyAccount();
});

function MyAccount(){
    this.queryUrl = '/fundOverview';
    this.queryInfo();
}

MyAccount.prototype.queryInfo = function(){
    Tool.ajax({
        url: this.queryUrl,
        type:"post"
    }).done(function(res){

        var data = res.data,
            accountInfo = data.accountInfo,
            accTradeInfoList = accountInfo.accTradeInfoList,
            accountArr = [];
        vm.isBinding = data.isBinding;
        vm.weixinMap = data.weixinMap;
        vm.accountInfo = accountInfo;
        accTradeInfoList.forEach(function(v,i) {
            var tabObj = {
                "text":v.platform+" 账户",
                "id":i,
                "currencyUnit": v.currency=='USD' ? '$' : '￥',
                "href":"javascript:;"
            };
            accountArr.push(avalon.mix(tabObj,v));
        });
        vm.accountTab.list = accountArr;
    });
}

/* 修改邮箱地址 */
function checkEmail(that){
    that.loading = true;
    Tool.ajax({
        url: '/setCustomerEmail',
        data:{
            email: that.email
        },
        type:"post"
    }).done(function(res){
        that.loading = false;

        var data = res.data,
            flag = data.flag,
            isAuto = data.isAuto,
            message = data.message;
        if(!flag){
            that.emailError = message;
        }else if(flag && !isAuto){
            that.display='none';
            layer.alert('申请已提交，请耐心等待，我们将尽快核实您的资料！');
        }else{
            that.display='none';
            vm.accountInfo.email = that.email;
            layer.alert('修改成功！');
        }
    });
}